package com.lyt.springboot.controller;

import com.lyt.springboot.dao.UserDataMapper;
import com.lyt.springboot.domain.UserData;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

public class MybatisTest {

    static Logger logger = Logger.getLogger(MybatisTest.class);

    @Test
    public void test() throws IOException {
        //1. 读取mybatis-config.xml 文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        //2. 构建SqlSessionFactory(创建了DefaultSqlSessionFactory)
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //3. 打开SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();

//        //4. 获取Mapper 接口对象   4和5应该是一起的
//        UserDataMapper mapper = sqlSession.getMapper(UserDataMapper.class);
//        //5. 获取mapper 接口对象的方法操作数据库
//        List<UserData> sysUsers = mapper.selectAll();

        //mybatis一级缓存
        //结合4,5步骤
        // --->这样查询和上面区别是  这里没有进行代理--》不走invoke方法:mapperMethod.execute(sqlSession, args)  所以在debug时会有区别要注意
        UserData userData = sqlSession.selectOne("com.lyt.springboot.dao.UserDataMapper.selectByPrimaryKey", 1);
        UserData userData1 = sqlSession.selectOne("com.lyt.springboot.dao.UserDataMapper.selectByPrimaryKey", 1);
        System.out.println("====");
        logger.info("查询结果为：" + userData);
        System.out.println("====");
        logger.info("查询结果2为：" + userData1);
    }


    @Test
    public void test1() throws IOException {
        //1. 读取mybatis-config.xml 文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        //2. 构建SqlSessionFactory(创建了DefaultSqlSessionFactory)
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


        //二级缓存
        //两个sqlsession
        //3. 打开SqlSession
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        UserData userData = sqlSession1.selectOne("com.lyt.springboot.dao.UserDataMapper.getById1", 1);
        System.out.println("====");
        logger.info("查询结果为：" + userData);
        System.out.println("====");
        sqlSession1.close();

        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        UserData userData1 = sqlSession2.selectOne("com.lyt.springboot.dao.UserDataMapper.getById1", 1);
        System.out.println("====");
        logger.info("查询结果2为：" + userData1);
        System.out.println("====");
        sqlSession2.close();

    }

    // 失败案例： 不同的sqlSessionFactory
    @Test
    public void testEnvironment() throws IOException {
        //1. 读取mybatis-config.xml 文件
        InputStream inputStream1 = Resources.getResourceAsStream("mybatis-config.xml");
        InputStream inputStream2 = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory sqlSessionFactory1 = new SqlSessionFactoryBuilder().build(inputStream1,"test");
        SqlSession sqlSession1 = sqlSessionFactory1.openSession();
        sqlSession1.selectOne("com.lyt.springboot.dao.UserDataMapper.getById1", 1);
        sqlSession1.close();

        SqlSessionFactory sqlSessionFactory2 = new SqlSessionFactoryBuilder().build(inputStream2,"test");
        SqlSession sqlSession2 = sqlSessionFactory2.openSession();
        sqlSession2.selectOne("com.lyt.springboot.dao.UserDataMapper.getById1", 1);
        sqlSession2.close();
    }

    //测试FeactSize流逝查询
    @Test
    public void testOom() {
//        init1();
        long beginTime = System.currentTimeMillis();

    }


}


